<template>
  <a-modal :maskClosable="false"
    :title='title'
    :width='600'
    :visible='visible'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'
  >
    <a-spin :spinning='confirmLoading'>
      <a-form-model ref='form' v-bind='layout' :model='model'>
        <a-form-model-item label='医生姓名' required prop='doctorId'>
          <a-input-search placeholder='请选择医生姓名' enter-button @search='handleSelectDoctor'
                          v-model='model.doctorName' />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <doctor-list-dialog ref="doctorModal" @signOk="handleSignChoice"></doctor-list-dialog>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addDict, editDict, duplicateCheck } from '@/api/api'
import DoctorListDialog from '@/views/his/dialog/EmployeeDialog'
import { httpAction } from '@api/manage'
export default {
  name: 'HPatientDoctorModal',
  components: {
    DoctorListDialog
  },
  props: {
     patientId: Number,
  },
  watch: {
    patientId(newValue) {
      this.getPatient(newValue);
    }
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {
        doctorName:"",
        doctorId:0,
      },
      layout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 14 }
      },
      confirmLoading: false,
      validatorRules: {
        dictName: [{ required: true, message: '请输入字典名称!' }],
        dictCode: [
          { required: true, message: '请输入字典编码!' },
          { validator: this.validateDictCode }]
      },
      url: {
        add: '/ch/patient/doctor',
      },
    }
  },
  created() {
  },
  methods: {
    getPatient(patientId){
      this.patientId = patientId
    },
    add(){
      this.model.doctorId=0;
      this.model.doctorName=""
      this.visible = true
    },
    edit(record) {
      this.visible = true
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          const memberId = this.patientId;
          this.model.memberId=memberId;
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleSignChoice(record) {
      this.model.doctorName = record.name
      this.model.doctorId = record.id
    },
    handleSelectDoctor() {
      this.$refs.doctorModal.show('1')
      this.$refs.doctorModal.title = '请选择医生信息'
      this.$refs.doctorModal.disableSubmit = false
    },
  }
}
</script>